{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import re\n",
    "def recog_class(string):    #定义一个从路径中读取label的函数\n",
    "    regex1 = re.compile(r\"class_\\d+\")    #定义 提取出类关键字 的正则表达式\n",
    "    str_num = re.findall(regex1,string)[-1]    #查找string中的 class_* 关键字\n",
    "    regex2 = re.compile(r\"class_*\")    #定义 从class_* 中提取数字的正则表达式\n",
    "    int_num=int(re.split(regex2,str_num)[-1])    #提取出label并转存为int型\n",
    "    return int_num\n",
    "def match_subject(string,subject_num):\n",
    "    regex = re.compile(r\"subject_*\")    #匹配subject_后的数字\n",
    "    int_num=int(re.split(regex,string)[-1])    #将匹配到的str型数字转为int型,即将数据切片得到最后的数字\n",
    "    if int_num <= subject_num:  #如果小于期望值\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "def match_item(string,floor,ceiling):    #定义一个匹配item的函数,可以规定上限和下限\n",
    "    regex = re.compile(r\"subject_*\")    #匹配subject_后的数字\n",
    "    int_num=int(re.split(regex,string)[-1])    #将匹配到的str型数字转为int型,即将数据切片得到最后的数字\n",
    "    print(string)\n",
    "    if int_num <= ceiling and int_num >= floor:  #如果小于期望值\n",
    "        return True\n",
    "    else:\n",
    "        print(\"Wrong floor or ceiling number!\")\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "def get_routefile(basepath,route_filename,subject_number):    #basepath是数据集的路径，filename是目标存储文件名\n",
    "    fopen = open(route_filename,'w')    #打开文件准备写入\n",
    "    classlist =  os.listdir(basepath)    #将basepath下的文件遍历并存入classlist\n",
    "    label = 0  \n",
    "    classlist.sort()    #按照类别的首字母排序\n",
    "    for classvar in classlist:    #for数据集下的每个类    \n",
    "        videodir = os.path.join('%s%s' % (basepath,classvar))\n",
    "        if os.path.isdir(videodir) == True:    #判断该条目是否是文件夹\n",
    "            if match_subject(videodir,subject_number)==True:   #判断条目是否符合筛选条件             \n",
    "                videolist = os.listdir(videodir)    #将video条目存入videolist\n",
    "                print(videolist)\n",
    "                fopen.write('%s\\n' % (videodir))    #将条目写入文件\n",
    "                #print videodir\n",
    "        else:\n",
    "            print videodir,\"is not a directory, skiped!\"\n",
    "            continue\n",
    "        label=label+1\n",
    "    fopen.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def train_route_label(route,trainroute,trainlabel,floor,ceiling):    #定义一个生成trainroute和trainlabel的函数，可以设定subject的个数\n",
    "    fopen = open(route,\"r\")    #以只读方式打开route.txt，用来读取全部item的路径\n",
    "    route_list = fopen.readlines()    #读取所有行存入route_list\n",
    "    fopen.close()    #关闭route.txt文件\n",
    "    f_route = open(trainroute,\"w\")    #以write方式打开trainroute.txt文件\n",
    "    f_label = open(trainlabel,\"w\")    #以write方式打开trainlabel.txt文件\n",
    "    for subject_item in route_list:     #对于route_list中的每个元素\n",
    "        if match_item(subject_item,floor=floor,ceiling=ceiling) == True:    #如果符合item筛选原则，就执行以下操作\n",
    "            class_label = recog_class(subject_item)-1   \n",
    "            f_route.write('%s' % (subject_item))    #将符合条件的item写入文件\n",
    "            f_label.write('%s\\n' % (class_label))   #将符合条件的item的label也写入文件\n",
    "            print subject_item    #打印item\n",
    "            print class_label    #打印label\n",
    "    f_route.close()    #关闭route文件\n",
    "    f_label.close()    #关闭label文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def test_route_label(route,testroute,testlabel,floor,ceiling):    #定义一个生成testroute和testlabel的函数，可以设定subject的个数\n",
    "    fopen = open(route,\"r\")    #以只读方式打开route.txt，用来读取全部item的路径\n",
    "    route_list = fopen.readlines()    #读取所有行存入route_list\n",
    "    fopen.close()    #关闭route.txt文件\n",
    "    f_route = open(testroute,\"w\")    #以write方式打开testroute.txt文件\n",
    "    f_label = open(testlabel,\"w\")    #以write方式打开testlabel.txt文件\n",
    "    print(route_list)\n",
    "    for subject_item in route_list:     #对于route_list中的每个元素\n",
    "        if match_item(subject_item,floor=floor,ceiling=ceiling) == True:    #如果符合item筛选原则，就执行以下操作\n",
    "            class_label = recog_class(subject_item)-1   \n",
    "            f_route.write('%s' % (subject_item))    #将符合条件的item写入文件\n",
    "            f_label.write('%s\\n' % (class_label))   #将符合条件的item的label也写入文件\n",
    "            print subject_item    #打印item\n",
    "            print class_label    #打印label\n",
    "    f_route.close()    #关闭route文件\n",
    "    f_label.close()    #关闭label文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "get_routefile() takes exactly 3 arguments (2 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-40-42ecf0df67e6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_routefile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/home/wjj/HDD/HDD/output_20170117_error/c3d/'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'route.txt'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;31m#train_route_label(\"route.txt\",\"trainroute.txt\",\"trainlabel.txt\",1,7)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;31m#test_route_label(\"route.txt\",\"testroute.txt\",\"testlabel.txt\",8,10)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: get_routefile() takes exactly 3 arguments (2 given)"
     ]
    }
   ],
   "source": [
    "get_routefile('/home/wjj/HDD/HDD/output_20170117_error/c3d/','route.txt',)\n",
    "#train_route_label(\"route.txt\",\"trainroute.txt\",\"trainlabel.txt\",1,7)\n",
    "#test_route_label(\"route.txt\",\"testroute.txt\",\"testlabel.txt\",8,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/samba/HDD/output_20170117_error/c3d/class_01_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_01_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_01_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_01_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_01_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_02_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_02_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_02_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_02_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_02_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_03_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_03_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_03_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_03_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_03_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_04_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_04_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_04_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_04_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_04_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_05_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_05_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_05_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_05_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_05_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_06_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_06_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_06_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_06_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_06_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_07_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_07_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_07_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_07_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_07_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_08_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_08_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_08_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_08_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_08_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_09_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_09_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_09_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_09_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_09_subject_05\\n', '/samba/HDD/output_20170117_error/c3d/class_10_subject_01\\n', '/samba/HDD/output_20170117_error/c3d/class_10_subject_02\\n', '/samba/HDD/output_20170117_error/c3d/class_10_subject_03\\n', '/samba/HDD/output_20170117_error/c3d/class_10_subject_04\\n', '/samba/HDD/output_20170117_error/c3d/class_10_subject_05\\n']\n",
      "/samba/HDD/output_20170117_error/c3d/class_01_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_01_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_01_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_01_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_01_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_02_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_02_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_02_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_02_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_02_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_03_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_03_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_03_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_03_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_03_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_04_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_04_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_04_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_04_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_04_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_05_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_05_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_05_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_05_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_05_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_06_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_06_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_06_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_06_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_06_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_07_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_07_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_07_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_07_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_07_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_08_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_08_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_08_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_08_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_08_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_09_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_09_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_09_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_09_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_09_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_10_subject_01\n",
      "\n",
      "(1, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_10_subject_02\n",
      "\n",
      "(2, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_10_subject_03\n",
      "\n",
      "(3, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_10_subject_04\n",
      "\n",
      "(4, 'wrong')\n",
      "Wrong floor or ceiling number!\n",
      "/samba/HDD/output_20170117_error/c3d/class_10_subject_05\n",
      "\n",
      "(5, 'wrong')\n",
      "Wrong floor or ceiling number!\n"
     ]
    }
   ],
   "source": [
    "test_route_label(\"route.txt\",\"testroute.txt\",\"testlabel.txt\",10,15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
